package com.example;

import lombok.Data;
import lombok.ToString;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * Lab3：Optional类的使用，包括以下API
 *
 * @author Holinc
 * @date 2023/3/21 20:30
 */
public class Lab3 {

    /**
     * of
     * get
     * isPresent
     * orElse
     * orElseGet
     * orElseThrow
     *
     * @param args
     */
    public static void main(String[] args) {
//        Optional<Object> opt = Optional.of(null);
        Optional<Object> nullOpt = Optional.ofNullable(null);
        Optional<User> opt = Optional.of(new User());
        if (opt.isPresent()) {
            User user = opt.get();
            System.out.println(user);
        }

        User zhangsan = new User("张三", 21);
        User lisi = new User("李四", 23);

        List<User> userList = new ArrayList<>();
        userList.add(zhangsan);
        userList.add(lisi);

        // 输出userList的所有userName
        List<String> userNameList = userList.stream().map(m -> m.getUsername()).collect(Collectors.toList());
        List<String> userNameList2 = userList.stream().map(User::getUsername).collect(Collectors.toList());

        userNameList.forEach(item-> System.out.println(item));
        userNameList2.forEach(item-> System.out.println(item));

        System.out.println("=================");


//        List<Integer> numbers = Arrays.asList(3, 2, 2, 3, 7, 3, 5);
//
//        IntSummaryStatistics stats = numbers.stream().mapToInt((x) -> x).summaryStatistics();
//
//
//        System.out.println("列表中最大的数 : " + stats.getMax());
//        System.out.println("列表中最小的数 : " + stats.getMin());
//        System.out.println("所有数之和 : " + stats.getSum());
//        System.out.println("平均数 : " + stats.getAverage());



        List<User> userList2 = Arrays.asList(zhangsan, lisi);
        Optional<User> userOptional = userList2.stream().filter(item -> item.getAge() > 22).findFirst();
        if (userOptional.isPresent()) {
            User user = userOptional.get();
            System.out.println(user.getUsername());
            System.out.println(user.getAge());
        }

        Optional<User> userOptional2 = userList2.stream().filter(item -> item.getAge() > 30).findFirst();

//        System.out.println(userOptional2.get());

        User wangwu = userOptional2.orElse(new User("王五", 31));
        System.out.println(wangwu.getUsername());



        Optional<User> userOptional4 = userList2.stream().filter(item -> item.getAge() > 30).findFirst();
        User user = userOptional2.orElseGet(() -> getOtherUser());
//        User user1 = userOptional2.orElseGet(Lab3::getOtherUser);
        System.out.println(user);


        Optional<User> userOptional3 = userList2.stream().filter(item -> item.getAge() > 30).findFirst();
        userOptional2.orElseThrow(() -> new RuntimeException("未找到符合条件的用户"));





//       final String separator = ",";
//
//        List<String> strings = Arrays.asList("a", "b", "c");
//        for (String s : strings) {
//            System.out.print(s + separator);
//            separator = new String("......");
//        }
//
//
//        Arrays.asList("a", "b", "c").forEach(
//                (String e) -> {
//                    System.out.print(e + separator);
//                    separator = new String("......");
//                });


        List<Integer> numbers = Arrays.asList(3, 2, 2, 3, 7, 3, 5);
        // 获取集合中大于2、并且经过排序、平方去重的有序集合
        List<Integer> squaresList = numbers
                .stream()
                .filter(x -> x > 2)
                .sorted((x, y) -> x.compareTo(y))
                .map(i -> i * i).distinct().collect(Collectors.toList());
        squaresList.forEach(item -> System.out.println(item));



    }

    static User getOtherUser() {
        return new User("John", 25);
    }

    @Data
    @ToString
    public static class User {
        private String username;
        private int age;

        public User() {

        }

        public User(String username, int age) {
            this.username = username;
            this.age = age;
        }

    }

}
